"""
-*- coding:utf-8 -*-
__project__ = 'gitee'
__file_name__ = 'studentSystem'
__author__ = '86138'
__time__ = '2020-11-17 19:56'
__product_name = PyCharm
# code is far away from bugs with the god animal protecting
    I love animals. They taste delicious.
              ┏┓      ┏┓
            ┏┛┻━━━┛┻┓
            ┃        ┃
            ┃  ┳┛  ┗┳  ┃
            ┃      ┻      ┃
            ┗━┓      ┏━┛
                ┃      ┗━━━┓
                ┃  神兽保佑    ┣┓
                ┃　永无BUG！   ┏┛
                ┗┓┓┏━┳┓┏┛
                  ┃┫┫  ┃┫┫
                  ┗┻┛  ┗┻┛
"""


def func_insert(list_info):
    str_name = input('输入name：\n')
    str_tel = input('输入tel：\n')
    str_qq = input('输入qq：\n')
    for info in list_info:
        if info['name'] == str_name:
            while True:
                str_yn = input('是否继续添加(y or n)：\n')
                if str_yn == 'y':
                    func_insert(list_info)
                    break
                elif str_yn == 'n':
                    print('取消成功')
                    break
                else:
                    print('输入错误！请重新输入')
            break
    else:
        list_info.append({'name': str_name, 'tel': str_tel, 'qq': str_qq})
        print('添加成功')
        while True:
            str_yn = input('是否继续添加(y or n)：\n')
            if str_yn == 'y':
                func_insert(list_info)
                break
            elif str_yn == 'n':
                print('取消成功')
                break
            else:
                print('输入错误！请重新输入')


def func_delete(list_info):
    str_name = input('输入删除人姓名：\n')
    for info in list_info:
        if info['name'] == str_name:
            while True:
                str_yn = input('确认删除(y or n)：\n')
                if str_yn == 'y':
                    list_info.remove(info)
                    print('删除成功')
                    while True:
                        str_yn = input('是否继续删除(y or n)：\n')
                        if str_yn == 'y':
                            func_delete(list_info)
                            break
                        elif str_yn == 'n':
                            print('取消成功')
                            break
                        else:
                            print('输入错误！请重新输入')
                    break
                elif str_yn == 'n':
                    print('取消成功')
                    break
                else:
                    print('输入错误！请重新输入')
            break
    else:
        print('查无此人')
        while True:
            str_yn = input('是否继续删除(y or n)：\n')
            if str_yn == 'y':
                func_delete(list_info)
                break
            elif str_yn == 'n':
                print('取消成功')
                break
            else:
                print('输入错误！请重新输入')


def func_update(list_info):
    str_name = input('输入修改人姓名：\n')
    for info in list_info:
        if info['name'] == str_name:
            print('你要修改的信息是：\n', info)
            str_name = input('输入修改后的name：\n')
            for i in list_info:
                if i['name'] == str_name:
                    print('已有此人！\n')
                    while True:
                        str_yn = input('是否继续修改(y or n)：\n')
                        if str_yn == 'y':
                            func_update(list_info)
                            break
                        elif str_yn == 'n':
                            print('退出成功！')
                            break
                        else:
                            print('输入错误！请重新输入')
                    break
            else:
                str_tel = input('输入修改后的tel：\n')
                str_qq = input('输入修改后的qq：\n')
                info['name'] = str_name
                info['tel'] = str_tel
                info['qq'] = str_qq
                print('修改成功')
                break
    else:
        print('查无此人')
        while True:
            str_yn = input('是否继续修改(y or n)：\n')
            if str_yn == 'y':
                func_update(list_info)
                break
            elif str_yn == 'n':
                print('退出成功！')
                break
            else:
                print('输入错误！请重新输入')


def func_select(list_info):
    str_name = input('输入查询人姓名：\n')
    for info in list_info:
        if info['name'] == str_name:
            print(info)
            while True:
                str_yn = input('是否继续查询(y or n)：\n')
                if str_yn == 'y':
                    func_select(list_info)
                    break
                elif str_yn == 'n':
                    print('取消成功')
                    break
                else:
                    print('输入错误！请重新输入')
            break
    else:
        print('查无此人')
        while True:
            str_yn = input('是否继续查询(y or n)：\n')
            if str_yn == 'y':
                func_select(list_info)
                break
            elif str_yn == 'n':
                print('取消成功')
                break
            else:
                print('输入错误！请重新输入')


def func_select_all(list_info):
    i = 1
    for info in list_info:
        print('id', i)
        print('name', info['name'])
        print('tel', info['tel'])
        print('qq', info['qq'])
        i += 1


def func_str():
    print("---------------------------")
    print("      学生管理系统 V1.0")
    print(" 1:添加学生")
    print(" 2:删除学生")
    print(" 3:修改学生")
    print(" 4:查询学生")
    print(" 5:显示所有学生")
    print(" 6:退出系统")
    print("---------------------------")


def func_main():
    fw = open('../test.txt', 'a', encoding='utf-8')
    fw.close()
    fr = open('../test.txt')
    list_info = fr.read()
    if list_info == '':
        list_info = [{'name': 'one', 'tel': 'a00000000001', 'qq': '0000000001'},
                     {'name': 'two', 'tel': 'a00000000002', 'qq': '0000000002'},
                     {'name': 'three', 'tel': 'a00000000003', 'qq': '0000000003'}]
    else:
        list_info = eval(list_info)
    fr.close()
    print(list_info)
    while True:
        func_str()
        str_id = input('请输入进行操作的序号\n')
        if str_id == '1':
            func_insert(list_info)
        elif str_id == '2':
            func_delete(list_info)
        elif str_id == '3':
            func_update(list_info)
        elif str_id == '4':
            func_select(list_info)
        elif str_id == '5':
            func_select_all(list_info)
        elif str_id == '6':
            while True:
                str_yn = input('是否确认退出(y：确认退出 or n：继续操作 )：\n')
                if str_yn == 'y':
                    fw = open('../test.txt', 'w', encoding='utf-8')
                    fw.write(str(list_info))
                    fw.close()
                    print('退出成功')
                    break
                elif str_yn == 'n':
                    func_main()
                    break
                else:
                    print('输入错误！请重新输入')
            break
        else:
            print('输入错误！请重新输入')


func_main()
